<?php
class ControllerCatalogManufacturer extends AdminController {

  	public function index() {
		M('catalog/manufacturer');
    	$this->getList();
  	}

  	public function insert() {

		M('catalog/manufacturer');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_catalog_manufacturer->addManufacturer($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/manufacturer'));
		}

    	$this->getForm();
  	}

  	public function update() {

		M('catalog/manufacturer');

    	if ($this->request->isPost() && $this->validateForm()) {
			$this->model_catalog_manufacturer->editManufacturer($this->request->get['manufacturer_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/manufacturer'));
		}

    	$this->getForm();
  	}

  	public function delete() {

		M('catalog/manufacturer');

    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $manufacturer_id) {
				$this->model_catalog_manufacturer->deleteManufacturer($manufacturer_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/manufacturer'));
    	}

    	$this->getList();
  	}

  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);
		
		$qf = new Query_filter();

		$manufacturer_total = $this->model_catalog_manufacturer->getTotalManufacturers();

		$this->data['manufacturers'] = $this->model_catalog_manufacturer->getManufacturers($qf);

		$this->data['pagination'] = $this->pagination($manufacturer_total)->render();

		$this->display('catalog/manufacturer_list.tpl');
	}

  	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);
		
		$manufacturer_id = G('manufacturer_id', 0);
		
		if ($manufacturer_id) {
			$this->data['action'] = UA('catalog/manufacturer/update','manufacturer_id=' . $manufacturer_id);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_catalog_manufacturer->getManufacturer($manufacturer_id);
			}
		} else {
			$this->data['action'] = UA('catalog/manufacturer/insert');
		}    	
		
		$this->data['token'] = $this->session->data['token'];

		M('setting/store');
		$this->data['stores'] = $this->model_setting_store->getStores();

		if (isset($this->request->post['manufacturer_store'])) {
			$this->data['manufacturer_store'] = $this->request->post['manufacturer_store'];
		} elseif ($manufacturer_id) {
			$this->data['manufacturer_store'] = $this->model_catalog_manufacturer->getManufacturerStores($manufacturer_id);
		} else {
			$this->data['manufacturer_store'] = array(0);
		}

		$this->display('catalog/manufacturer_form.tpl');
	}

  	protected function validateForm() {
		$this->load->library('form_validation', true);
		$this->form_validation->set_rules('name', '', 'required|range_length[3,64]', $this->_['error_name']);
		return $this->form_validation->run();

  	}

  	protected function validateDelete() {

		M('catalog/product');

		foreach ($this->request->post['selected'] as $manufacturer_id) {
  			$product_total = $this->model_catalog_product->getTotalProductsByManufacturerId($manufacturer_id);

			if ($product_total) {
	  			$this->setMessage('error_warning', sprintf($this->_['error_product'], $product_total));
				return false;
			}
	  	}
		return true;
  	}

	public function autocomplete() {
		$json = array();

		if (isset($this->request->get['filter_name'])) {
			M('catalog/manufacturer');

			$results = $this->model_catalog_manufacturer->getManufacturers(new Query_filter(null, false));

			foreach ($results as $result) {
				$json[] = array(
					'manufacturer_id' => $result['manufacturer_id'],
					'name'            => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
				);
			}
		}

		$sort_order = array();

		foreach ($json as $key => $value) {
			$sort_order[$key] = $value['name'];
		}

		array_multisort($sort_order, SORT_ASC, $json);

		$this->response->setOutput(json_encode($json));
	}
}
?>